package com.example.houduan.controller;

import com.example.houduan.Entity.User;
import com.example.houduan.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/api/login")
    public Map<String, Object> login(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String password = params.get("password");

        Map<String, Object> result = new HashMap<>();

        if (username == null || password == null) {
            result.put("code",  400);
            result.put("message",  "用户名或密码不能为空");
            return result;
        }

        User user = userService.login(username,  password);
        if (user == null) {
            result.put("code",  400);
            result.put("message",  "用户名或密码错误");
            return result;
        }

        result.put("code",  200);
        result.put("message",  "登录成功");
        result.put("data",  user);
        return result;
    }
}